package c04.thread.safe;

import java.util.ArrayList;

class ThreadSafe {
    static final int THREAD_NUMBER = 2;
    static final int LOOP_NUMBER = 200;

    public static void main(String[] args) {
        ThreadSafe t = new ThreadSafe();
        for (int i = 0; i < THREAD_NUMBER; i++) {
            new Thread(() -> {
                t.testAddRemove(LOOP_NUMBER);
            }, "Thread" + (i+1)).start();
        }
    }

    public void testAddRemove(int loopNumber) {
        ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < loopNumber; i++) {
            add(list);
            remove(list);
        }
    }

    public void add(ArrayList<String> list) {
        list.add("1");
    }

    public void remove(ArrayList<String> list) {
        System.out.println(1);
        list.remove(0);
    }
}